/* eslint-disable eqeqeq *///忽略==的警告
import { meshLog } from "../config";
//16进制求和
export const sumHexBytes = (hexStr) => {
  let sum = 0;
  for (let i = 0; i < hexStr.length; i += 2) {
    // 每两个字符作为一个字节处理
    let byte = hexStr.slice(i, i + 2);
    // 将16进制字符串转换为10进制数
    sum += parseInt(byte, 16);
  }
  let decimalNumber = sum;
  let hexString = decimalNumber.toString(16);
  let lastTwoDigits = (hexString.toString().slice(-2));
  //meshLog('hexString', hexString);
  return lastTwoDigits;

}

//两个字节，反转
export const getOctets2Msg = (oldNum) => {
  let mStr = oldNum.toString(16) + '';
  if (mStr.length == 1) {
    mStr = '0' + mStr + '00';
  } else if (mStr.length == 2) {
    mStr = mStr + '00';
  } else if (mStr.length == 3) {
    let str = '0' + mStr;
    mStr = str.substring(2, 4) + str.substring(0, 2);
  } else if (mStr.length == 4) {
    let str = mStr;
    mStr = str.substring(2, 4) + str.substring(0, 2);
  }
  return mStr;
}
//int转16进制 转成2个数
export const transIntToHex2Str = (num) => {
  let str = (num.toString(16) + '').toLocaleUpperCase();
  if (str.length == 1) {
    str = '0' + str;
  }
  return str;
}
//0609需要的 转成4个数
export const transIntToHex4Str = (num) => {
  let str = (num.toString(16) + '').toLocaleUpperCase();
  if (str.length == 1) {
    str = '000' + str;
  } else if (str.length == 2) {
    str = '00' + str;
  } else if (str.length == 3) {
    str = '0' + str;
  }
  return str;
}

//mac地址翻转
export const bigEndianToLittleEndian = (hexStr) => {
  // 确保字符串长度是偶数
  if (hexStr.length % 2 !== 0) {
    throw new Error('Input string must have an even length');
  }

  // 将十六进制字符串分解为字节数组
  const bytes = [];
  for (let i = 0; i < hexStr.length; i += 2) {
    bytes.push(parseInt(hexStr.substr(i, 2), 16));
  }

  // 反转字节数组以实现从大端到小端的转换
  const littleEndianBytes = bytes.reverse();

  // 将字节数组转换回十六进制字符串
  let result = '';
  for (const byte of littleEndianBytes) {
    result += byte.toString(16).padStart(2, '0');
  }

  return result.toUpperCase(); // 返回大写的十六进制字符串
}
// //时间转换，从mqtt中需要减掉946684800
// export const mqttTimestamp = (timestamp) => {


// }

//16进制数如何转换为负数十进制
export const hexToNegativeDecimal = (hexStr) => {
  // 移除前缀并统一大写
  hexStr = hexStr.replace(/^0x/i, '').toUpperCase();

  // 判断是否为负数（首位8-F）
  const isNegative = /^[8-9A-F]/.test(hexStr[0]);

  if (!isNegative) {
    return parseInt(hexStr, 16);
  }

  // 计算补码（取反+1）
  const bits = hexStr.length * 4;
  const maxValue = Math.pow(2, bits);
  const rawValue = parseInt(hexStr, 16);

  // 补码公式：value - 2^n
  return rawValue - maxValue;
}



//上面我修改的

//int转16进制
export const getMaxTrimInfo = (mMaxStr) => {
  let info = '100%';
  if (typeof (mMaxStr) == 'undefined' || mMaxStr == 'FFFF' || mMaxStr == '') {
    info = '100%'
  } else {
    let m = (parseInt(mMaxStr, 16) / 655.35);
    info = parseInt(m + 0.5) + '%'
  }
  return info;
}
//yyyyMMddHHmmss
export const exportTimeStr = () => {
  let time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let day = time.getDate();
  let hour = time.getHours();
  let min = time.getMinutes();
  let sec = time.getSeconds();
  let str = year + transIntTo2Str(month) + transIntTo2Str(day) + transIntTo2Str(hour) + transIntTo2Str(min) + transIntTo2Str(sec);
  meshLog("wjk time:" + str);
  return str;
}
export const transIntTo2Str = (num) => {
  let str = num + '';
  if (str.length == 1) {
    str = '0' + str;
  }
  return str;
}
